<template>
  <div>
    <div class="warehouse" v-if="!warehouseAddShow">
      <!-- 筛选按钮 -->
      <div class="search-button">
        <el-button
          v-hasPermi="['system:warehouse:query']"
          class="btn search"
          @click="getWarehouseList"
          ><el-icon class="icon" color="#fff"> <Search /> </el-icon
          >查询</el-button
        >
        <el-button
          v-hasPermi="['system:warehouse:add']"
          class="btn search"
          @click="
            warehouseAddShow = true;
            pageType = 'add';
          "
          ><el-icon class="icon" color="#fff"> <Plus /> </el-icon
          >新增</el-button
        >
        <el-button class="btn add" @click="handleRefresh"
          ><el-icon class="icon" color="#fff"> <Refresh /> </el-icon
          >重置</el-button
        >
        <el-popover :width="170" trigger="click" placement="bottom">
          <template #reference>
            <el-button class="btn search" style="width: 100px"
              ><el-icon class="icon" color="#fff"> <Setting /> </el-icon
              >显示列</el-button
            >
          </template>
          <el-checkbox-group v-model="checkList" @change="handleCheckChange">
            <div>
              <el-checkbox label="warehouseCode">仓库编号</el-checkbox>
            </div>
            <div>
              <el-checkbox label="warehouseName">仓库名称</el-checkbox>
            </div>
            <div>
              <el-checkbox label="companyName">所属公司</el-checkbox>
            </div>
            <div>
              <el-checkbox label="warehouseAddress">仓库地址</el-checkbox>
            </div>
            <div>
              <el-checkbox label="name">仓库负责人</el-checkbox>
            </div>
            <div>
              <el-checkbox label="phone">联系方式</el-checkbox>
            </div>
          </el-checkbox-group>
        </el-popover>
      </div>
      <!-- 筛选条件 -->
      <div class="search">
        <div class="search-item">
          <el-input
            clearable
            v-model.trim="params.warehouseName"
            class="search-input"
            maxlength="30"
            placeholder="请输入仓库名称"
          ></el-input>
        </div>
      </div>

      <!-- 表格数据 -->
      <el-table
        v-loading="loading"
        :data="warehouseList"
        border
        height="64vh"
        :header-cell-style="{ 'text-align': 'center', background: 'red' }"
        @sort-change="sortChange"
      >
        <!-- 操作模式 -->
        <el-table-column
          label="操作"
          :show-overflow-tooltip="true"
          width="140px"
        >
          <template #default="{ row }">
            <div class="table-operate">
              <el-icon
                v-hasPermi="['system:warehouse:edit']"
                title="编辑"
                color="#FFB700"
                class="table-icon"
                :size="20"
                @click="handleOperate('edit', row)"
              >
                <Edit />
              </el-icon>
              <el-icon
                v-hasPermi="['system:warehouse:detail']"
                title="查看详情"
                color="#007CEF"
                class="table-icon"
                :size="20"
                @click="handleOperate('detail', row)"
              >
                <Document />
              </el-icon>
              <el-icon
                v-hasPermi="['system:warehouse:remove']"
                title="删除"
                color="#E33838"
                class="table-icon"
                :size="20"
                @click="handleOperate('delete', row)"
              >
                <Delete />
              </el-icon>
            </div> </template
        ></el-table-column>
        <el-table-column
          type="index"
          label="序号"
          :index="indexMethod"
          width="70"
          align="center"
        />
        <el-table-column v-if="checkList.includes('warehouseCode')"
          label="仓库编号 "
          prop="warehouseCode"
          :show-overflow-tooltip="true"
          min-width="80"
          sortable="custom"
        />
        <el-table-column v-if="checkList.includes('warehouseName')"
          label="仓库名称"
          prop="warehouseName"
          :show-overflow-tooltip="true"
          min-width="80"
          sortable="custom"
        />
        <el-table-column v-if="checkList.includes('companyName')"
          label="所属公司"
          prop="companyName"
          :show-overflow-tooltip="true"
          min-width="100"
          sortable="custom"
        />

        <el-table-column v-if="checkList.includes('warehouseAddress')"
          label="仓库地址"
          prop="warehouseAddress"
          :show-overflow-tooltip="true"
          min-width="120"
          sortable="custom"
        />
        <el-table-column v-if="checkList.includes('name')"
          label="仓库负责人"
          prop="name"
          :show-overflow-tooltip="true"
          min-width="80"
          sortable="custom"
        />
        <el-table-column v-if="checkList.includes('phone')"
          label="联系方式"
          prop="phone"
          :show-overflow-tooltip="true"
          min-width="100"
          sortable="custom"
        />
      </el-table>

      <!-- 分页 -->
      <pagination
        class="pagination"
        :total="total"
        v-model:page="params.pageNum"
        v-model:limit="params.pageSize"
        @pagination="getWarehouseList"
      />
    </div>
    <WarehouseAdd
      v-else
      ref="warehouseAddRef"
      v-model:warehouseAddShow="warehouseAddShow"
      @refreshList="getWarehouseList"
      :pageType="pageType"
    />
  </div>
</template>

<script setup>
import WarehouseAdd from "./warehouseAdd.vue";
import {
  getWarehouseListApi,
  deleteWarehouseApi,
} from "@/api/information/warehouse.js";
import { addTableHeaderApi, getTableHeaderApi } from "@/api/pub";
import { ElMessage, ElMessageBox } from "element-plus";
// 搜索参数
const params = reactive({
  warehouseName: "",
  pageNum: 1,
  pageSize: 50,
  sortField: "",
  sortOrder: "",
});

const checkList = ref([
  "warehouseCode",
  "warehouseName",
  "companyName",
  "warehouseAddress",
  "name",
  "phone",
]);

// 表格加载效果
const loading = ref(false);
// 仓库新增组件显隐
const warehouseAddShow = ref(false);
// 仓库列表
const warehouseList = ref([]);
// 总数
const total = ref(0);
// 仓库新增组件引用
const warehouseAddRef = ref(null);
// 操作类型
const pageType = ref("");


/**
 * @description: 复选框选中
 */
 const handleCheckChange = async (val) => {
  var str = val.join(",");
  const result = await addTableHeaderApi({
    moduleType: 5,
    titleField: str,
  });
};

/**
 * @description: 获取表头
 */
const getTableHeader = async () => {
  const result = await getTableHeaderApi({
    moduleType: 5,
  });
  if (result.code == 200) {
    var arr = result.data.titleField.split(",");
    checkList.value = arr;
  }
};

/**
 * @description: 获取仓库列表
 */
const getWarehouseList = async () => {
  try {
    loading.value = true;
    let { warehouseName, pageNum, pageSize,sortField ,sortOrder} = params;
    const res = await getWarehouseListApi(
      { pageNum, pageSize },
      {
        warehouseName,
        sortField,
        sortOrder,
      }
    );
    warehouseList.value = res.rows;
    total.value = res.total;
    loading.value = false;
  } catch {
    loading.value = false;
  }
};

/**
 * @description: 列表排序
 * @param {*} column.prop //当前排序取的字段
 * @param {*} column.order // "desc" "asc" 正序或倒序
 */
 const sortChange = (column) => {
  params.sortField = column.prop;
  if (column.order === "descending") {
    params.sortOrder = "DESC"; // 排序或倒序
  } else {
    params.sortOrder = "ASC"; // 排序或倒序
  }
  getWarehouseList();
};

/**
 * @description: 重置
 */
 const handleRefresh = () => {
  Object.assign(params, {
    warehouseName: "",
    pageNum: 1,
    pageSize: 50,
    sortField: "",
  sortOrder: "",
  });
  getWarehouseList()
};

/**
 * @description: 表格操作
 * @param {String} type 类型
 * @param {Object} row  当前行数据
 */
const handleOperate = async (type, row) => {
  switch (type) {
    case "edit":
      pageType.value = type;
      warehouseAddShow.value = true;
      nextTick(() => {
        warehouseAddRef.value.handleDetailInfo(row.warehouseId);
      });
      break;
    case "detail":
      pageType.value = type;
      warehouseAddShow.value = true;
      nextTick(() => {
        warehouseAddRef.value.handleDetailInfo(row.warehouseId);
      });
      break;

    case "delete":
      ElMessageBox.confirm("此操作将删除该仓库，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
        type: "warning",
      })
        .then(async () => {
          const res = await deleteWarehouseApi(row.warehouseId);
          if (res.code === 200) {
            ElMessage.success(res.msg);
            getWarehouseList();
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
      break;

    default:
      break;
  }
};

/**
 * @description: 序号
 * @param {Number} index 索引
 */
function indexMethod(index) {
  const currentPage = params.pageNum;
  const pageSize = params.pageSize;
  const truelndex = (currentPage - 1) * pageSize + index + 1;
  return truelndex;
}

onMounted(() => {
  getTableHeader();
  getWarehouseList();
});
</script>
<style>
@import "@/assets/styles/confirmBtn.css";
</style>
<style lang="scss" scoped>
.warehouse {
  padding: 20px;
  width: 100%;
  height: 100%;
  .pagination {
    margin-top: 20px;
  }
}
</style>
